package com.synthefact.daj.model.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.synthefact.daj.model.ChannelIf;
import com.synthefact.daj.model.GlobalAssertionIf;
import com.synthefact.daj.model.NetworkIf;
import com.synthefact.daj.model.NodeIf;
import com.synthefact.daj.model.ProgramIf;
import com.synthefact.daj.model.ProgramStatus;


/**
 * A program executes on a node.
 * 
 * <p>
 * Copyright (C) 2011 Synthefact
 * <p>
 * Based on code from (c) 1997, Wolfgang Schreiner <Wolfgang.Schreiner@risc.uni-linz.ac.at>
 * http://www.risc.uni-linz.ac.at/software/daj
 */
public abstract class Program implements ProgramIf {

    protected NodeIf iNode;

    @Override
    public void setNode(NodeIf node) {
	iNode = node;
    }

    @Override
    public ProgramStatus getStatus() {
	// the default behavior is NONE
	return ProgramStatus.NONE;
    }

    @Override
    public ChannelIf getInChannel(int inChannelIndex) {
	return iNode.getInChannel(inChannelIndex);
    }

    @Override
    public Set<ChannelIf> getInChannelSet() {
	return iNode.getInChannelSet();
    }

    @Override
    public ChannelIf getOutChannel(int outChannelIndex) {
	return iNode.getOutChannel(outChannelIndex);
    }

    @Override
    public Set<ChannelIf> getOutChannelSet() {
	return iNode.getOutChannelSet();
    }

    public void terminate() {
	iNode.setReadyToEnd();
    }

    @Override
    public void test(GlobalAssertionIf globalAssertion) {
	// XXX - this is weird, digging into all the nodes to pass all the programs?
	NetworkIf network = iNode.getNetwork();
	Set<NodeIf> nodes = network.getNodes();
	List<ProgramIf> programs = new ArrayList<ProgramIf>();
	for (NodeIf node: nodes) {
	    programs.add(node.getProgram());
	}
	globalAssertion.test(programs);
    }

    @Override
    public abstract void main();

}
